package com.example.springboot_first.service;

import com.example.springboot_first.dao.UserDao;
import com.example.springboot_first.pojo.User;
import com.example.springboot_first.pojo.dto.UserDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService implements IUserService{//调用数据访问类
    @Autowired
    UserDao userDao;
    @Override
    public User add(UserDto user) {
        User userPojo=new User();
        BeanUtils.copyProperties(user,userPojo);
        return userDao.save(userPojo);

    }

    @Override
    public User getUser(Integer userid) {
        return userDao.findById(userid).orElseThrow(()->{
            throw new IllegalArgumentException("用户不存在");
        });
    }

    @Override
    public User edit(UserDto user) {
        User userPojo=new User();
        BeanUtils.copyProperties(user,userPojo);
        return userDao.save(userPojo);
    }

    @Override
    public void delete(Integer userid) {
        userDao.deleteById(userid);
    }
}
